package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.ProgramaDAO;
//import com.cs.beneficios.business.entity.RepBeneficioOtorgadoDAO;
//import com.cs.beneficios.business.entity.RepDetBeneficiarioDAO;
import com.cs.beneficios.business.entity.ReporteTotalProgramasDAO;
import com.cs.beneficios.business.entity.SedeDAO;
import com.cs.beneficios.business.entity.SubProgramaDAO;
import com.cs.beneficios.business.entity.SubprogramaSedeDAO;
import com.cs.beneficios.business.model.ProgramaVO;
//import com.cs.beneficios.business.model.RepBeneficioOtorgadoVO;
//import com.cs.beneficios.business.model.RepDetBeneficiarioVO;
//import com.cs.beneficios.business.model.ReporteTotalProgramasVO;
import com.cs.beneficios.business.model.SubProgramaVO;
//import com.cs.beneficios.business.model.SubprogramaSedeVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.paoyer.administration.business.model.UserVO;
import java.sql.SQLException;
import java.util.Collection;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class UsuarioSubprogramaAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    Object codePrograma =  (Object)request.getParameter("codePrograma");
    Object codeSubprograma = request.getParameter("codeSubPrograma");
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    HttpSession session = request.getSession();
    UserVO user = (UserVO)session.getAttribute("userInfo");
    session.removeAttribute("msg");
    
    session.setAttribute("msg","");
    Collection programas=null;
    Collection subprogramas=null;
    Collection sedes=null;
    ProgramaDAO objProgramaDAO;
    SubProgramaDAO objSubProgramaDAO;
    SedeDAO objSedeDAO;
    SubprogramaSedeDAO objSubsedeDAO;
    ReporteTotalProgramasDAO  objProg;
    session.removeAttribute("Beneficios");
    
    Object obAccion = request.getParameter("accion");
    session.removeAttribute("programas");
    session.removeAttribute("subprogramas");
    session.removeAttribute("sedes");

    
    try
    {
      objProgramaDAO = new ProgramaDAO(cf);
      programas = objProgramaDAO.findProgramaByCriterea(new ProgramaVO(),user.getCode(),user.getUserGroupName(),user.getCodeArea());
      cf.closeConnection(objProgramaDAO.getConn());
    }catch(SQLException e){
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSSPE - cwsub ********/");
    }
    session.removeAttribute("msg");
    if(programas.size() > 0){
      session.setAttribute("programas",programas);
    }else
    {
      session.setAttribute("msg","No se encontraron programas vigentes a la fecha..");
    }
      
    if(codePrograma != null)
    {
     
      String codpro = (String)codePrograma;
      if(!codpro.equals("nulo"))
      {
        try
        {
          objSubProgramaDAO = new SubProgramaDAO(cf);
          SubProgramaVO subpro = new SubProgramaVO();
          subpro.setCodePrograma(new Long((String)codePrograma));
          subprogramas = objSubProgramaDAO.findSubProgramaByCriterea(subpro, user.getCode(),user.getUserGroupName(),user.getCodeArea());
          cf.closeConnection(objSubProgramaDAO.getConn());
        }catch(SQLException e){
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
        session.removeAttribute("msg");
        if(subprogramas.size() > 0){
          session.setAttribute("subprogramas",subprogramas);
        }else
        {
          session.setAttribute("msg","No se encontraron subprogramas asociados a este programa que esten vigentes..");
        }
        
        if(codeSubprograma != null)
        {
          String codsubpro = (String)codeSubprograma;
          if(!codsubpro.equals("nulo"))
          {
            
            try
            {
              objSedeDAO = new SedeDAO(cf);
              sedes = objSedeDAO.GetTotalSedebyUser(user.getCode(), new Long(codsubpro),user.getUserGroupName(),user.getCodeArea());
              cf.closeConnection(objSedeDAO.getConn());
            }catch(SQLException e){
				System.err.println("/******** TDSUB - cwsub");
				e.printStackTrace();
				System.err.println("TDSSPE - cwsub ********/");
            }
            if(sedes.size() > 0)
            {
              session.setAttribute("sedes",sedes);
        
        
            }else
            {
              session.setAttribute("msg","No se encontraron sedes vigentes a la fecha..");
            }
            
          }
        }
      }else{
          session.setAttribute("msg","");
          session.removeAttribute("subprogramas");
      }
    }
    
    return mapping.findForward("success");
    
  }
}